package com.loadmore.oneday

import android.Manifest
import android.os.Bundle
import android.os.Handler
import android.text.TextUtils
import android.view.View
import com.alibaba.android.arouter.launcher.ARouter
import com.loadmore.common.App
import com.loadmore.common.base.BaseActivity
import com.loadmore.common.constants.Api
import com.loadmore.common.constants.Configs
import com.loadmore.common.constants.Constants
import com.loadmore.common.image.ImageLoader
import com.loadmore.common.image.ImageUtil
import com.loadmore.common.model.BaseResponse
import com.loadmore.common.model.User
import com.loadmore.common.net.Request
import com.loadmore.common.net.impl.JsonCallback
import com.loadmore.common.permission.PermissionCallback
import com.loadmore.common.router.RouterPath
import com.loadmore.common.util.InitUtil
import com.loadmore.common.util.SPFUtil
import com.loadmore.common.variables.Variables
import com.lzy.okgo.model.Response
import com.qmuiteam.qmui.util.QMUIViewHelper
import kotlinx.android.synthetic.main.activity_welcome.*

class WelcomeActivity : BaseActivity() {

    private var handler = Handler()
    private var hasPermission = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_welcome)
        setSwipeBackEnable(false)
        QMUIViewHelper.fadeIn(welcomeText,3000,null,true)
        hideActionBar()
        initPermission()
    }

    private fun initPermission() {
        requestPermission(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE),object :PermissionCallback{
            override fun onSettingBack() {

                
            }

            override fun onPermissionsDenied() {
            }

            override fun onPermissionsGranted() {
                init()
            }
        })
    }

    private fun init() {

        displayWelcomeImage()
        InitUtil.initVariables()
        hasPermission = true
        handler.postDelayed({
            if (TextUtils.isEmpty(Variables.token)) {
                ARouter.getInstance().build(RouterPath.Login).navigation()
                finish()
            }else{
               checkLogin()
            }
        },3000)
    }
    private fun displayWelcomeImage() {
        var index = Math.random().times(20).toInt() +1
        ImageLoader.load(this, ImageUtil.getUrl(index.toString()),welcomeImage)
        handler.postDelayed({
            skip.visibility = View.VISIBLE
            QMUIViewHelper.fadeIn(welcomeImage,1000,null,true)
        },1000)

        skip.setOnClickListener {
            handler.removeCallbacksAndMessages(null)
            checkLogin()
        }
    }
    private fun checkLogin() {
        Request.post(Api.info, hashMapOf<String,Any>("token" to Variables.token!!),object : JsonCallback<BaseResponse<User>>(){
            override fun onSuccess(response: Response<BaseResponse<User>>?) {
                var data = response?.body()
                if (data?.code == 200 && data.data != null && data.data != null) {
                    var user = data?.data!!
                    App.setDataBase(this@WelcomeActivity, user!!.id!!)
                    App.daoSession.insertOrReplace(user)
                    Variables.user = user
                    SPFUtil(Configs.User).putLong(Constants.user_id,user!!.id)
                    ARouter.getInstance().build(RouterPath.Main).navigation()
                    finish()
                }

            }

            override fun onError(response: Response<BaseResponse<User>>?) {
                super.onError(response)
                ARouter.getInstance().build(RouterPath.Login).navigation()
                finish()
            }

        })
    }

    override fun onBackPressed() {
        //super.onBackPressed()
        if (!hasPermission) {
            super.onBackPressed()
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        handler.removeCallbacksAndMessages(null)
    }
}
